package com.sgl.gk.controller;

import com.sgl.common.annotation.Log;
import com.sgl.common.core.controller.BaseController;
import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.core.page.TableDataInfo;
import com.sgl.common.enums.BusinessType;
import com.sgl.common.utils.ServletUtils;
import com.sgl.common.utils.StringUtils;
import com.sgl.gk.domain.GkEquipment;
import com.sgl.gk.service.GkEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:xec
 * @date:2022/10/31 18:50
 *
 * 设备表的controller
 **/
@RestController
@RequestMapping("/gk/equipment")
public class GkEquipmentController extends BaseController {

    @Autowired
    GkEquipmentService gkEquipmentService;

    /**
     * 按条件分页查询设备信息
     * @param gkEquipment  查询条件
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(GkEquipment gkEquipment){
        if(ServletUtils.getParameter("pageSize")!=null&&ServletUtils.getParameter("pageNum")!=null){
            startPage();
        }
        List<GkEquipment> list = gkEquipmentService.list(gkEquipment);
        return getDataTable(list);
    }

    /**
     * 添加设备
     * @param gkEquipment
     * @return
     */
    @Log(title = "设备",businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody GkEquipment gkEquipment){
        if (gkEquipment.getStationId()==null||StringUtils.isEmpty(gkEquipment.getIp())||StringUtils.isEmpty(gkEquipment.getPort())||StringUtils.isEmpty(gkEquipment.getSerialPortName())){
            return AjaxResult.error("非空验证不通过");
        }
        if (gkEquipment.getIp().length()>25||gkEquipment.getPort().length()>10||gkEquipment.getSerialPortName().length()>100){
            return AjaxResult.error("输入ip、端口号或串口号过长");
        }
        return gkEquipmentService.add(gkEquipment);
    }

    /**
     * 修改设备
     * @param gkEquipment
     * @return
     */
    @Log(title = "设备",businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody GkEquipment gkEquipment){
        if (gkEquipment.getStationId()==null||StringUtils.isEmpty(gkEquipment.getIp())||StringUtils.isEmpty(gkEquipment.getPort())||StringUtils.isEmpty(gkEquipment.getSerialPortName())){
            return AjaxResult.error("非空验证不通过");
        }
        if (gkEquipment.getIp().length()>25||gkEquipment.getPort().length()>10||gkEquipment.getSerialPortName().length()>100){
            return AjaxResult.error("输入ip、端口号或串口号过长");
        }
        return gkEquipmentService.edit(gkEquipment);
    }

    /**
     * 删除设备
     * @param ids
     * @return
     */
    @Log(title = "设备" ,businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult removeByIds(@PathVariable("ids") Long[] ids){
        return gkEquipmentService.removeByIds(ids);
    }
}
